package com.zhr.tea.view.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.CarBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 毛懿 on 2016/10/31.
 */

public class FenleiTuijianAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    @Override
    public void onClick(View view) {
        if (recyClickListener != null) {
            recyClickListener.onItemClick((Integer) view.getTag());

        }
    }

    private RecyClickListener recyClickListener = null;

    public interface RecyClickListener {
        void onItemClick(int title);
    }


    public void setOnItemClickListener(RecyClickListener r) {
        this.recyClickListener = r;
    }


    private Context context;
    private List<CarBean.ContentBean> data;

    public static final int TYPE_HEAD = -1;
    public static final int TYPE_CONTENT = -2;


    public FenleiTuijianAdapter(Context context, List<CarBean.ContentBean> data) {
        this.context = context;
        this.data = data;

    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (getItemViewType(position) == TYPE_HEAD) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD:
                view = LayoutInflater.from(context).inflate(R.layout.item_fenleituijian_head, parent, false);
                view.setOnClickListener(this);
                return new HeadViewHolder(view);
            case TYPE_CONTENT:
                view = LayoutInflater.from(context).inflate(R.layout.item_car_foot, parent, false);
                view.setOnClickListener(this);
                return new ItemViewHolder(view);
        }
        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            ((HeadViewHolder) holder).tv1.setText("您输入的关键字不符合");
            ((HeadViewHolder) holder).tv2.setText("为您精心推荐以下商品");
        } else if (holder instanceof ItemViewHolder){
            Glide.with(context).load(data.get(position - 1).getGoodsImgUrl()).placeholder(R.mipmap.abc_ic_menu_copy_mtrl_am_alpha).into(((ItemViewHolder) holder).carFootIv);
            ((ItemViewHolder) holder).carFootTvTitle.setText(data.get(position - 1).getGoodsName());
            ((ItemViewHolder) holder).carFootTvPrice.setText("￥" + data.get(position - 1).getGoodsPrice());
            ((ItemViewHolder) holder).carFootTvCount.setText("已售" + data.get(position - 1).getSaleCount() + "份");
             holder.itemView.setTag(data.get(position-1).getGoodsId());
        }
    }


    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;

        public HeadViewHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.tv1);
            tv2 = (TextView) itemView.findViewById(R.id.tv2);
        }
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_foot_iv)
        ImageView carFootIv;
        @BindView(R.id.car_foot_tv_title)
        TextView carFootTvTitle;
        @BindView(R.id.car_foot_tv_price)
        TextView carFootTvPrice;
        @BindView(R.id.car_foot_tv_count)
        TextView carFootTvCount;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
